package com.zhuangxiaoyan.springboot.mybatis.controller;

import com.zhuangxiaoyan.springboot.mybatis.domain.Product;
import com.zhuangxiaoyan.springboot.mybatis.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ProductController
 *
 * @author xjl
 * @version 2025/01/11 19:47
 **/
@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    // 添加产品
    @PostMapping
    public int addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    // 更新产品
    @PutMapping("/{id}")
    public int updateProduct(@PathVariable long id, @RequestBody Product product) {
        product.setId(id);
        return productService.updateProduct(product);
    }

    // 删除产品
    @DeleteMapping("/{id}")
    public int deleteProduct(@PathVariable long id) {
        return productService.removeProduct(id);
    }

    // 获取产品 by ID
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable long id) {
        return productService.getProductById(id);
    }

    // 获取所有产品
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }
}